package com.sdx.console.content.web;


import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import java.util.Collections;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.sdx.common.utils.JacksonUtils;
import org.apache.commons.lang3.StringUtils;

import org.beetl.sql.core.engine.PageQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import com.ibeetl.admin.core.annotation.Function;

import com.ibeetl.admin.core.web.JsonResult;

import com.ibeetl.admin.core.util.*;

import com.sdx.console.content.entity.*;
import com.sdx.console.content.service.*;
import com.sdx.console.content.web.query.*;

/**
 * 标签管理 接口
 */
@RequestMapping("/tag/resTag")
@Controller
public class ResTagController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(ResTagController.class);
    private static final String MODEL = "/tag/resTag";


    @Resource
	private ResTagService resTagService;

    /* 页面 */

    @GetMapping("/index.do")
    @Function("tag.resTag.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        return view;
    }

    @GetMapping("/edit.do")
    @Function("tag.resTag.edit")
    @ResponseBody
    public ModelAndView edit(Integer tagId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        ResTag resTag = resTagService.queryById(tagId);
        view.addObject("resTag", resTag);
		try {
			view.addObject("resTagJson", JacksonUtils.toJsonString(resTag,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("tag.resTag.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("tag.resTag.query")
    @ResponseBody
    public JsonResult<PageQuery> list(ResTagQuery condtion)
    {
        PageQuery page = condtion.getPageQuery();
        resTagService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,ResTag resTag,HttpServletRequest request
	) {
	    CoreUser user = getLoginUser(request);
    	JsonResult result = JsonResult.success();
    	if (isNew) {
    		resTag.setTagStatus(1);
			resTag.setCreatedBy(user.getId().intValue());
			resTag.setCreatedName(user.getName());
			resTag.setCreatedTime(new Date());
			resTagService.save(resTag);
		} else {
    		resTag.setUpdatedBy(user.getId().intValue());
    		resTag.setUpdatedName(user.getName());
    		resTag.setUpdatedTime(new Date());
			resTagService.updateTemplate(resTag);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("tag.resTag.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)ResTag resTag,HttpServletRequest request
		)
    {

        return save(true,resTag,request
		);
    }

    @PostMapping("/update.json")
    @Function("tag.resTag.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  ResTag resTag,HttpServletRequest request
		) {
		return save(false,resTag,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("tag.resTag.query")
    @ResponseBody
    public JsonResult<ResTag>queryInfo(Integer tagId) {
        ResTag resTag = resTagService.queryById( tagId);
        return  JsonResult.success(resTag);
    }

    @PostMapping("/delete.json")
    @Function("tag.resTag..del")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        //List<Long> idList = ConvertUtil.str2longs(ids);
		List<String> idList = new ArrayList<String>();
		Collections.addAll(idList,ids.split(","));
        resTagService.batchDelResTag(idList);
        return JsonResult.success();
    }
    

}
